<?php
include_once '../inc/config_inc.php';
include_once '../inc/mysql_inc.php';
include_once '../inc/tool_inc.php';

$link=connect();
if(isset($_POST['submit'])){
	$query="select * from link where name='{$_POST['name']}'";
	$result=execute($link,$query);
	if(mysqli_num_rows($result)){
		skip('link_add.php','error','该名称已经存在!');exit();
	}else{
		$_POST=escape($link,$_POST); //转义
		$query="insert into link(name,url,sort) values('{$_POST['name']}','{$_POST['adress']}',{$_POST['sort']})";
		execute($link,$query);
		if(mysqli_affected_rows($link)==1){
			skip('link.php','ok','恭喜你，添加成功!');exit();
		}else{
			skip('link_add.php','error','对不起添加失败,请重试!');exit();
		}
	}	
}

$template['title']='增加友情链接'; 
$template['css']=array('style/public.css');
?>

<?php include 'inc/header_inc.php'; ?>

<div id="main">
	<div class="title" style="margin-bottom: 20px;">添加友情链接</div>
	<form method="post" onsubmit="return check()">
		<table class="au">
			<tr>
				<td>名称</td>
				<td><input name="name" type="text" id="name"></td>
				<td>名称不得为空，最大不得超过20个字符</td>
			</tr>
			<tr>
				<td>url地址</td>
				<td><input name="adress" type="text" id="adress"></td>
				<td>地址不得为空，最大不得超过50个字符</td>
			</tr>
			<tr>
				<td>排序</td>
				<td><input name="sort" value="0"　type="text" id="sort"/></td>
				<td>填写一个数字即可</td>
			</tr>
		</table>
		<input style="margin-top:20px; cursor: pointer;" class="btn" type="submit" name="submit" value="添加" />
	</form>

<script type="text/javascript">
	function check(){
		var n=document.getElementById('name').value;
		var s=document.getElementById('sort').value;
		var a=document.getElementById('adress').value;
		if(n===""){
			alert("名称不能为空!");
			return false;
		}else{
			if(n.length>20){
				alert("最大不得超过20个字符!");
				return false;
			}
		}
		if(a===""){
			alert("地址不能为空!");
			return false;
		}else{
			if(a.length>50){
				alert("最大不得超过50个字符!");
				return false;
			}
		}
		if(s===""){
			alert("排序不得为空!");
			return false;
		}else{
			if(isNaN(s)){
				alert("排序只能是数字!");
				return false;
			}
		}
	}
</script>

</div>

<?php include 'inc/footer_inc.php'; ?>